﻿package keyfun.core
{
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.net.navigateToURL;
	import flash.utils.ByteArray;
	
	public class GlobalFunction
	{
		// constant data
		public static const BITS_PER_MB:Number = 1024 * 1000;
		public static var actionCount:int = 0;
		
		public static function genId():String
		{
			actionCount++;
			return String(new Date().getTime() + actionCount);
		}
		
		public static function validEmail(str:String):Boolean
        {
            var pattern:RegExp = /(\w|[_.\-])+@((\w|-)+\.)+\w{2,4}+/;
            var result:Object = pattern.exec(str);
            if (result == null)
            {
                return false;
            }
            return true;
        }
		
		public static function getMB(n:Number):Number
		{
			return n / BITS_PER_MB;
		}
		
		public static function isImage(type:String):Boolean
		{
			type = type.toLowerCase();
			if(type == ".jpg" || type == ".png")
			{
				return true;
			}
			return false;
		}

		public static function isVideo(type:String):Boolean
		{
			type = type.toLowerCase();
			if(type == ".mp4" || type == ".wmv" || type == ".avi" || type == ".mpg" || type == ".mpeg" || type == ".flv")
			{
				return true;
			}
			return false;
		}
		
		// resize to max if exceed
		public static function resizeObject(obj:Object, maxW:Number, maxH:Number):void 
		{
			//trace("resize max");
			// calculation ratio to which resize takes 
			var r:Number = obj.height / obj.width;
			
			if (r <= 1)
			{
				if (obj.width > maxW)
				{
					obj.width = maxW;
					obj.height = Math.round(obj.width * r);
				}
				
				if (obj.height > maxH)
				{
					obj.height = maxH;
					obj.width = Math.round(obj.height / r);
				}
			}
			else
			{
				if (obj.height > maxH)
				{
					obj.height = maxH;
					obj.width = Math.round(obj.height / r);
				}
				
				if (obj.width > maxW)
				{
					obj.width = maxW;
					obj.height = Math.round(obj.width * r);
				}
			}
		}
		
		public static function checkInput(str:String):Boolean
		{
			if(str.indexOf("]]>") != -1)
			{
				return false;
			}
			else
			{
				return true;
			}
		}
		
		public static function removeAllChilds(obj:Object):void
		{
			for(var i:int = obj.numChildren - 1; i >= 0; i--)
			{
				obj.removeChildAt(i);
			}
		}
		
		public static function completeFadeIn(o:Object):void
		{
			// do nth
		}
		
		public static function completeFadeOut(o:Object):void
		{
			o.visible = false;
		}
		
		public static function convertDate(dateStr:String, format:String="yyyy-mm-dd"):String
		{
			var returnDateStr:String;
			var dateArr:Array = splitDate(dateStr);
			var year:String = dateArr[0];
			var month:String = dateArr[1];
			var day:String = dateArr[2];
			
			switch(format)
			{
				case "yyyy-mm-dd":
				returnDateStr = year + "-" + updateDate(month) + "-" + updateDate(day);
				break;
				
				case "d/m/yyyy":
				returnDateStr = Number(day) + "/" +  Number(month) + "/" + year;
				break;
				
				default:
				returnDateStr = dateStr;
				break;
			}
			return returnDateStr;
		}
		
		public static function updateDate(str:String):String
		{
			if(str.length < 2)
			{
				str = "0" + str;
			}
			return str;
		}
		
		public static function splitDate(dateStr:String):Array
		{
			var tmpArr:Array;
			if(dateStr.indexOf("-") != -1)
			{
				tmpArr = dateStr.split("-");
			}
			else if(dateStr.indexOf("/") != -1)
			{
				tmpArr = dateStr.split("/");
			}
			
			if(tmpArr[0].length < 4)
			{
				tmpArr.reverse();
			}
			return tmpArr;
		}
		
		public static function setHitLayer(mc:MovieClip):void
		{
			var hiddenLayer:Sprite = new Sprite();
			hiddenLayer.graphics.beginFill(0xFFFFFF, 0);
			hiddenLayer.graphics.drawRect( -2, -2, mc.width + 4, mc.height + 4);
			hiddenLayer.graphics.endFill();
			mc.addChild(hiddenLayer);
		}
		
		public static function setText(tf:TextField, str:Object, defaultStr:String=""):void
		{
			if (tf == null)
			{
				return;
			}
			
			if (str != null)
			{
				tf.text = String(str);
			}
			else
			{
				tf.text = defaultStr;
			}
		}
		
		public static function clone(source:Object):* 
		{
		    var copier:ByteArray = new ByteArray();
    		copier.writeObject(source);
    		copier.position = 0;
    		return(copier.readObject());
		}
		
		public static function getColorToStr(color:uint):String
		{
			return ("0x" + color.toString(16));
		}
		
		public static function getColorToInt(colorStr:String, hasAlpha:Boolean = false):uint
		{
			var pattern:RegExp = /#/;
			var color:uint = uint(colorStr.toString().replace(pattern,"0x"));
			
			var max:Number = ( hasAlpha ) ? 0xFFFFFFFF : 0xFFFFFF;
			return uint( Math.min( Math.max( color, 0 ), max ) );
		}
		
	}
}